#include "clientlist.h"

//fonction locale
static int isequal(client_p client1, client_p client2);


clientlist_p clientlist_new(void){
	clientlist_p list;
	list = malloc(sizeof(clientlist_t));
	if (list == NULL)
		return NULL;
	clientlist_init(list);
	return list;
}

void clientlist_init(clientlist_p list){
	list->first = NULL;
	list->last = NULL;
	list->size = 0;
}


return_t clientlist_insert(clientlist_p list, client_p dataclient){
	if(list != NULL){
		elementclient *new;
		new = malloc(sizeof(elementclient));
		if (new == NULL)
			return RT_ERR;
		new->data = dataclient;

		//ajout de l'elt en debut de liste
		new->previous = NULL;
		if(list->size == 0){
			new->next = NULL;
			list->first = new;
			list->last = new;
		}else{
			list->first->previous = new;
			new->next = list->first;
			list->first = new;
		}
		list->size++;
		return RT_OK;
	}
	fprintf(stdout, "liste nulle\n");
	return RT_ERR;
}

return_t clientlist_remove(clientlist_p list, client_p dataclient){
	if (list != NULL){
		elementclient *tmp_elt = list->first;

		//parcours de la liste
		while (tmp_elt != NULL){
			if (tmp_elt->data == dataclient){
				if (tmp_elt->next == NULL){ //elt trouve en fin de list
					list->last = tmp_elt->previous;
					list->last->next = NULL;
				} else if (tmp_elt->previous == NULL){ // elt trouve en debut de list
					list->first = tmp_elt->next;
					list->first->previous = NULL;
				} else {
					tmp_elt->previous->next = tmp_elt->next;
					tmp_elt->next->previous = tmp_elt->previous;
				}
				if (tmp_elt->data != NULL)
					free(tmp_elt->data);
				free(tmp_elt);
				list->size--;
				return RT_OK;
			} else
				tmp_elt = tmp_elt->next;
		}
	}
	return RT_ERR;
}

return_t clientlist_clear(clientlist_p list){
	if (list != NULL){
		elementclient *tmp_elt = list->first;
		while(tmp_elt != NULL){
			elementclient *del_elt = tmp_elt;
			tmp_elt = tmp_elt->next;
			if (del_elt->data != NULL)
				free(del_elt->data);
			free(del_elt);
		}
		list->first = NULL;
		list->last = NULL;
	}
	return(RT_OK);
}

void clientlist_delete(clientlist_p list){
	if (list != NULL){
		elementclient *tmp_elt = list->first;
		while(tmp_elt != NULL){
			elementclient *del_elt = tmp_elt;
			tmp_elt = tmp_elt->next;
			if (del_elt->data != NULL)
				free(del_elt->data);
			free(del_elt);
		}
		free(list);
		list = NULL;
	}
}

void clientlist_print(clientlist_p list){
	if (list != NULL){
		if (list->size != 0){
			elementclient *tmp_elt = list->first;
			while (tmp_elt != list->last){
				fprintf(stdout, "(client) ip : %s, port serveur = %d, port client = %d, socket = %d is_client = %d\n",
						tmp_elt->data->ip, tmp_elt->data->port_s, tmp_elt->data->port_c, tmp_elt->data->socket, tmp_elt->data->isclient);
				tmp_elt = tmp_elt->next;
			}
			//afficher dernier element
			fprintf(stdout, "(client) ip : %s, port serveur = %d, port client = %d, socket = %d is_client = %d\n",
					tmp_elt->data->ip, tmp_elt->data->port_s, tmp_elt->data->port_c, tmp_elt->data->socket, tmp_elt->data->isclient);
		} else
			fprintf(stdout, "liste vide\n");
	}
}

client_p findBySocket(int socket,clientlist_p list){
	if (list->size !=0){
		elementclient *elt = list->last;
		while(elt != NULL){
			if(elt->data->socket == socket)
				return elt->data;
			elt = elt->previous;
		}
	}
	return NULL;
}

int isequal(client_p client1, client_p client2){
	if((strcmp(client1->ip, client2->ip)==0) && client1->port_s == client2->port_s )
		return 1;
	return 0;
}

int checkClientExist(clientlist_p list,client_p client){
	if (list->size != 0){
		elementclient *elt = list->last;
		while(elt != NULL){
			if(isequal(elt->data, client))
				return 1 ;
			elt = elt->previous;
		}
	}
	return 0;
}

